﻿Ext.ns('Portal.mailerScreen');

Ext.apply(Portal.mailerScreen, {

    //functie de stergere / editare inregistrare
    action: function () {
        var oGrid, iRec = 0, oWind, op = "edit";

        oGrid = Ext.getCmp(Portal.mailerScreen.id).getComponent("mailerGrid");

        //setare tip operatie; daca este butonul de adaugare / editare atunci se citeste paramentrul `action` al acestuia
        if (this.action) {
            op = this.action;
        }

        if (op === "edit") {
            if (oGrid.selModel.getCount() != 1) {
                Portal.events.throwInformation("Selectati o inregistrare !");
                return;
            }
            iRec = oGrid.selModel.getSelected().id;
        }


        //constructie formular
        var oFpAdd = new Ext.FormPanel({
            autoHeight: true, //inaltimea formului va da inaltimea ferestrei
            labelWidth: 160,
            frame: true,
            bodyStyle: {
                padding: "5px"
            },
            layout: 'form',
            defaults: {
                xtype: "textfield",
                allowBlank: false
            },
            items: [
                {
                    name: 'MailerName',
                    fieldLabel: 'Nume Mailer'  ,
                    width : 200
                },
                {
                    xtype:'xdatetime' ,
                    width : 200,
                    name:'StartDateL',
                    fieldLabel:'Data' ,
                    //anchor:'-18',
                    timeFormat:'H:i:s',
                    timeConfig: {
                        altFormats:'H:i:s',
                        allowBlank:false
                    } ,
                    dateFormat:'Y-m-d'   ,
                    dateConfig: {
                        altFormats:'Y-m-d|Y-n-d'   ,
                        allowBlank:false
                    }
                } ,
                {
                    xtype : "htmleditor",
                    name : "MailerBodyL",
                    height : 200,
                    anchor : "95%",
                    fieldLabel: "Body"
                },
                {
                    fieldLabel: 'Campanie',
                    xtype: "uxFCombo",
                    width : 200,
                    name: "IdCampaign",
                    forceSelection: false,
                    minChars: 2,
                    easyConfig: {
                        baseParams: {
                        },
                        proxyUrl: 'Campaign/GetCampaign/'
                    }
                } ,
                {
                    fieldLabel: "Status",
                    xtype: "uxFCombo",
                    editable: false,
                    name: "Status",
                    easyConfig: {
                        mode: "local",
                        localData: [
                            ['0', 'In asteptare'],
                            ['1', 'In derulare'],
                            ['2', 'Finalizat']
                        ]
                    }
                },
                {
                    name: "Id",
                    xtype: "hidden",
                    value: iRec
                }
            ],
            buttons: [
                {
                    text: 'Salvare',
                    handler: function () {

                        if (!oFpAdd.getForm().isValid()) {
                            return;
                        }

                        oFpAdd.getForm().submit({
                            url: "Mailer/EditMailer",
                            success: function (form, response) {
                                oGrid.store.reload();
                                oWind.close();
                            }
                        });
                    }
                }
            ]
        });

        //constructie fereastra
        oWind = new Ext.Window({
            title: (op === "edit") ? "Editare inregistrare" : "Adaugare inregistrare",
            width: 800,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            items: [oFpAdd]
        }).show();

        //daca este editare de inregistrare atunci se aduc datele acestei inregistrari
        if (op === "edit") {
            oFpAdd.on("afterlayout", function () {
                        Ext.Ajax.request({
                            url: 'Mailer/GetMailerById',
                            params: {
                                Id: iRec
                            },
                            success: function (response) {
                                var oRes = Ext.decode(response.responseText);
                                var oData = oRes.Records;
                                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
                            }
                        })
                    }, this, { single: true });
        }
    }
});

Ext.apply(Portal.mailerScreen, {

    id: 'idTabmailerScreen',
    exec: function () {

        if (!Ext.getCmp(Portal.mailerScreen.id)) {

            var oCmmailer = new Ext.grid.ColumnModel({
                columns: [
                    {
                        header: "Nume Mailer",
                        dataIndex: 'MailerName',
                        width: 100
                    },
                    {
                        header: "Data trimiterii",
                        dataIndex: 'StartDateL',
                        width: 100
                    },
                    {
                        header: "Campanie",
                        dataIndex: 'CampaignName'
                    },
                    {
                        header: "Trmimiteri Succes",
                        dataIndex: 'SendSuccess'  ,
                        renderer: Portal.renderer.greenColor
                    }
                    ,
                    {
                        header: "Trimiteri Eroare",
                        dataIndex: 'SendFailed'   ,
                        renderer: Portal.renderer.redColor
                    },
                    {
                        header: "Status",
                        dataIndex: 'Status',
                        renderer: Portal.renderer.mailerStatus
                    }
                ],
                defaults: {
                    sortable: true
                }
            });

            var omailerGrid = new Ext.ux.fastGrid({
                xtype: "uxFGrid",
                layout: 'fit',
                itemId: "mailerGrid",
                region: 'center',
                gridConfig: {
                    url: 'Mailer/GetMailer',
                    fields: ['IdMailer', 'MailerName', 'StartDateL','CampaignName','SendSuccess','SendFailed','Status']
                },
                cm: oCmmailer,
                tbar: [
                    {
                        text: "Adauga",
                        iconCls: 'icon-add',
                        action: "add",
                        handler: Portal.mailerScreen.action
                    },
                    "-",
                    {
                        text: "Editeaza",
                        iconCls: 'icon-cog-edit',
                        action: "edit",
                        handler: Portal.mailerScreen.action
                    }
                ],
                listeners: {
                    'rowdblclick': Portal.mailerScreen.action
                }
            });

            //creare tab ce contine gridul
            var oNomTab = new Ext.Panel({
                title: 'Mailer',
                id: Portal.mailerScreen.id,
                iconCls: 'icon-group',
                closable: true,
                layout: 'border',
                items: [omailerGrid]
            });

            //adaugare tab la tabpanel-ul principal
            Portal.oCenterRegion.add(oNomTab);
            Portal.oCenterRegion.doLayout();

        }
        //activare tab
        Portal.oCenterRegion.setActiveTab(Portal.mailerScreen.id);
    }
});